package com.enjoyor.health.HGPT.web.rest;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enjoyor.health.HGPT.core.config.ParamConfig;
import com.enjoyor.health.HGPT.core.dto.ReservationWeekDto;
import com.enjoyor.health.HGPT.core.dto.ReservationWeekDtoV2;
import com.enjoyor.health.HGPT.core.entity.FileInfo;
import com.enjoyor.health.HGPT.core.entity.PhysicalExaminationPackage;
import com.enjoyor.health.HGPT.core.service.IPhysicalExaminationPackageService;
import com.enjoyor.health.HGPT.service.BizFileService;
import com.enjoyor.health.HGPT.service.BizReservationService;
import com.enjoyor.health.HGPT.web.CodeEnum;
import com.enjoyor.health.HGPT.web.RetResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * web页的预约管理接口
 * @author Zhu Min
 * @date 2023/11/1 11:43
 */
@Api(value = "预约页面", tags = {"Web页面管理接口"})
@CrossOrigin
@RequestMapping("/rest")
@RestController
public class WebReservationController {

    @Autowired
    private BizReservationService bizReservationService;
    @Autowired
    private IPhysicalExaminationPackageService physicalExaminationPackageService;
    @Autowired
    private ParamConfig paramConfig;
    @Autowired
    private BizFileService bizFileService;

    /**
     * 获取预约配置
     */
    @ApiOperation(value = "预约号数，一周按时段")
    @RequestMapping(value = "/reserve/getReservationConfig", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult getReservationConfig( String type){
        ReservationWeekDto dto = bizReservationService.getReservationConfigByPackageType(type);
        return new RetResult().setResult(dto);
    }

    /**
     * 获取当天预约配置
     */
    @ApiOperation(value = "获取一天的预约配置V2")
    @RequestMapping(value = "/reserve/getReservationConfigDay", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult getReservationConfigDay(String weekDay){
        return new RetResult().setResult(bizReservationService.getReservationConfigByWeekDay(weekDay));
    }

    /**
     * 编辑当天预约配置V2
     */
    @ApiOperation(value = "编辑一天的预约配置V2")
    @PostMapping(value = "/reserve/editReservationConfigDay")
    public RetResult editReservationConfigDay(@RequestBody ReservationWeekDtoV2.ReservationDayDto reservationDayDto){
        bizReservationService.updateReservationConfig(reservationDayDto);
        return new RetResult();
    }

    /**
     * 编辑预约配置
     */
    @Deprecated
    @ApiOperation(value = "编辑当天")
    @PostMapping(value = "/reserve/editReservationConfig")
    public RetResult editReservationConfig(@RequestBody ReservationWeekDto.ReservationDayDto reservationDayDto, String packageType){
        if (reservationDayDto.getIndex() == null || StringUtils.isBlank(packageType) || CollUtil.isEmpty(reservationDayDto.getPeriodMap())){
            return new RetResult(CodeEnum.PARAM_MISS);
        }else {
            bizReservationService.updateReservationConfig(reservationDayDto, Integer.parseInt(packageType));
            return new RetResult();
        }
    }


    /**
     * 体检包增删改查
     */
    @RequestMapping(value = "/examPkg/getExamPackage", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult getExamPackage(){
        return new RetResult().setResult(physicalExaminationPackageService.list());
    }
    @PostMapping(value = "/examPkg/delExamPackage")
    public RetResult delExamPackage(@RequestBody PhysicalExaminationPackage physicalExaminationPackage){
        physicalExaminationPackageService.removeById(physicalExaminationPackage.getId());
        return new RetResult().setResult(physicalExaminationPackage.getId());
    }
    @PostMapping(value = "/examPkg/updateExamPackage")
    public RetResult updateExamPackage(@RequestBody PhysicalExaminationPackage physicalExaminationPackage){
        PhysicalExaminationPackage pkg = physicalExaminationPackageService.getById(physicalExaminationPackage.getId());
        if (StringUtils.isNotBlank(physicalExaminationPackage.getName())){
            pkg.setName(physicalExaminationPackage.getName());
        }
        if (physicalExaminationPackage.getPackageType() != null){
            PhysicalExaminationPackage.PhyExamEnum phyExamEnum = PhysicalExaminationPackage.PhyExamEnum.getByPkgType(physicalExaminationPackage.getPackageType());
            if (phyExamEnum == null){
                return new RetResult(CodeEnum.ERROR);
            }
            pkg.setPackageType(physicalExaminationPackage.getPackageType());
            pkg.setPackageTypeName(phyExamEnum.getPackageTypeName());
            pkg.setCompany(phyExamEnum.getCompany());
        }
        if (physicalExaminationPackage.getType() != null){
            pkg.setType(physicalExaminationPackage.getType());
        }
        if (physicalExaminationPackage.getGender() != null){
            pkg.setGender(physicalExaminationPackage.getGender());
        }
        physicalExaminationPackageService.updateById(pkg);
        return new RetResult().setResult(pkg);
    }
    @PostMapping(value = "/examPkg/addExamPackage")
    public RetResult addExamPackage(@RequestBody PhysicalExaminationPackage physicalExaminationPackage){
        physicalExaminationPackage.setId(IdUtil.simpleUUID());
        PhysicalExaminationPackage.PhyExamEnum phyExamEnum = PhysicalExaminationPackage.PhyExamEnum.getByPkgType(physicalExaminationPackage.getPackageType());
        if (phyExamEnum == null){
            return new RetResult(CodeEnum.ERROR);
        }
        physicalExaminationPackage.setPackageTypeName(phyExamEnum.getPackageTypeName());
        physicalExaminationPackage.setCompany(phyExamEnum.getCompany());
        physicalExaminationPackageService.save(physicalExaminationPackage);
        return new RetResult().setResult(physicalExaminationPackage);
    }


    @RequestMapping(value = "/aiHealthReport/reservationInfoWeb", method = { RequestMethod.GET, RequestMethod.POST })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pkgType", value = "体检包类型，可为空查询全部类型，1-银江全身报告,2-银江展会体检包,3-全身疾病筛查"),
            @ApiImplicitParam(name = "startDate", value = "查询时间段起始，如2023-01-01，可为空"),
            @ApiImplicitParam(name = "endData", value = "查询时间段终止，如2023-12-31，可为空"),
            @ApiImplicitParam(name = "period", value = "查询时间段字符串，可为空：\"10:00-11:00\", \"11:00-12:00\", \"13:00-14:00\", \"14:00-15:00\", \"15:00-16:00\", \"16:00-17:00\""),
            @ApiImplicitParam(name = "status", value = "状态，可为空，1-已体检，0-未体检"),
            @ApiImplicitParam(name = "page", value = "页码，不为空"),
            @ApiImplicitParam(name = "size", value = "每页条数，不为空")})
    public RetResult getReservationWeb(
            String pkgType,
            String startDate,
            String endData,
            String period,
            String status,
            @RequestParam(required = true, defaultValue = "1") int page,
            @RequestParam(required = true, defaultValue = "10")int size){
        JSONArray object = new JSONArray();
        Page<Map<String, Object>> list = bizReservationService.reservationWeb(pkgType,
                startDate,
                endData,
                period,
                status, page, size);
        return new RetResult().setResult(list);
    }

    @RequestMapping(value = "/aiHealthReport/checkIn", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult checkIn(String reservationNumber){
        bizReservationService.updateReservationInfoPresentTime(reservationNumber);
        return new RetResult();
    }

}
